package com.guava;

import java.util.Random;
import java.util.concurrent.Callable;

public class ListableFuture {

	public static void main(String[] args) throws Exception {
		// Future<String> future =
		/*
		 * List<Future<String>> results = new ArrayList<Future<String>>();
		 * ExecutorService es = Executors.newCachedThreadPool(); for(int i=0; i<3;i++) {
		 * Future<String> future = es.submit(new MyCallable("第"+i+"线程"));
		 * results.add(future); } while (results.size() > 0) { Thread.sleep(1000);
		 * System.out.println("剩余"+results.size()+"未执行"); for (int i = 0; i <
		 * results.size(); i++) { Future<String> res = results.get(i); if (res.isDone())
		 * { String str = res.get(); System.out.println(str); results.remove(res); } } }
		 * es.shutdown(); System.out.println("线程执行结束");
		 */
		 
	}

	 
	static class MyCallable implements Callable<String> {
		String name;

		public MyCallable(String name) {
			this.name = name;
		}

		@Override
		public String call() throws Exception {
			try {
				int nextInt = new Random().nextInt(10000);
				Thread.sleep(nextInt);
				return "第" + name + "线程，睡眠：" + nextInt + " 秒";
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return "";
		}

	}
}
